library(tidyverse)
library(cowplot)
library(pdftools)
load(here::here('data/data.RData'))

data %>%
  mutate(
    SO = case_when(
      SO == "Austral: Brazilian Journal of Strategy and International Relations" ~ "Austral",
      SO == "Brazilian Journal of International Relations" ~ "BJIR",
      SO == "Conjuntura Austral: Journal of the Global South" ~ "Conjuntura Austral",
      SO == "Estudos Internacionais: Revista de Relações Internacionais da PUC Minas" ~"Estudos Internacionais",
      SO == "Meridiano 47 - Journal of Global Studies" ~ "Meridiano 47",
      SO == "Monções: Revista de Relações Internacionais da UFGD" ~ "Monções",
      SO == "Oikos - Revista de Economia Politica Internacional" ~ "Oikos",
      SO == "Revista Brasileira de Estudos de Defesa" ~ "RBED",
      SO == "Revista Brasileira de Politica Internacional" ~ "RBPI",
      TRUE ~ SO
      
    )
  ) %>% 
  filter(PY < 2022) -> data

data %>%
  separate_rows(OG, sep = ";") %>%
  distinct() %>%
  mutate(OG = str_trim(OG)) %>%
  filter(OG != "NA" & !is.na(OG)) %>%
  mutate(OG = if_else(OG == "Puc Minas","PUC/MG",OG),
         OG = if_else(OG == "PUC/Bras?lia","UCB",OG)) -> data_OG

data %>%
  filter(!str_detect(OG, "NA") & !is.na(OG)) %>%
  group_by(SO) %>%
  count() %>%
  rename(
    "n_articles" = n
  ) -> n_artices_per_SO_notNA

data_OG %>%
  filter(!str_detect(OG, "NA") & !is.na(OG)) %>%
  group_by(SO,OG) %>%
  count() %>%
  ungroup() %>%
  group_by(SO) %>%
  count() %>%
  rename("n_distinct_og" = n) -> n_distinct_OG

data %>%
  filter(SO == "Meridiano 47" & PY > 2015) %>%
  filter(!str_detect(OG, "NA") & !is.na(OG)) %>%
  group_by(SO) %>%
  count() %>%
  rename(
    "n_articles" = n
  ) -> n_artices_per_SO_notNA_meridiano


data_OG %>%
  filter(!str_detect(OG, "NA") & !is.na(OG)) %>%
  filter(SO == "Meridiano 47" & PY > 2015) %>%
  group_by(SO,OG) %>%
  count() %>%
  ungroup() %>%
  group_by(SO) %>%
  count() %>%
  rename("n_distinct_og" = n) -> n_distinct_OG_meridiano



#####################################


data_OG %>%
  distinct() %>%
  filter(PY < 2005) %>%
  group_by(OG) %>%
  count() %>%
  arrange(desc(n)) %>%
  head(10) %>%
  ggplot(aes(x = n, y = reorder(OG,-n))) +
  geom_col(fill = "#444444") +
  geom_text(
    aes(label = n),
    hjust = 1.4,
    col = "white",
    fontface = "bold",
    size = 3
  ) +
  labs(
    x = "#",
    y = ""
  ) +
  theme_minimal() +
  scale_x_continuous(limits = c(0,120), breaks = c(0,120)) +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) -> plot8_3a



data_OG %>%
  distinct() %>%
  filter(PY > 2005 & PY < 2010) %>%
  group_by(OG) %>%
  count() %>%
  arrange(desc(n)) %>%
  head(10) %>%
  ggplot(aes(x = n, y = reorder(OG,-n))) +
  geom_col(fill = "#444444") +
  geom_text(
    aes(label = n),
    hjust = 1.4,
    col = "white",
    fontface = "bold",
    size = 3
  ) +
  labs(
    x = "#",
    y = ""
  ) +
  theme_minimal() +
  scale_x_continuous(limits = c(0,260), breaks = c(0,260)) +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) -> plot8_3b


data_OG %>%
  distinct() %>%
  filter(PY > 2009 & PY < 2015) %>%
  group_by(OG) %>%
  count() %>%
  arrange(desc(n)) %>%
  head(10) %>%
  ggplot(aes(x = n, y = reorder(OG,-n))) +
  geom_col(fill = "#444444") +
  geom_text(
    aes(label = n),
    hjust = 1.4,
    col = "white",
    fontface = "bold",
    size = 3
  ) +
  labs(
    x = "#",
    y = ""
  ) +
  theme_minimal() +
  scale_x_continuous(limits = c(0,125), breaks = c(0,125)) +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) -> plot8_3c


data_OG %>%
  distinct() %>%
  filter(PY > 2014) %>%
  group_by(OG) %>%
  count() %>%
  arrange(desc(n)) %>%
  head(10) %>%
  ggplot(aes(x = n, y = reorder(OG,-n))) +
  geom_col(fill = "#444444") +
  geom_text(
    aes(label = n),
    hjust = 1.4,
    col = "white",
    fontface = "bold",
    size = 3
  ) +
  labs(
    x = "#",
    y = ""
  ) +
  theme_minimal() +
  scale_x_continuous(limits = c(0,160), breaks = c(0,160)) +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) ->  plot8_3d


plot_grid(
  plot8_3a,plot8_3b, plot8_3c, plot8_3d,
  nrow = 2,
  ncol = 2,
  labels = c("1997-2004","2005-2009","2010-2014","2015-2021"),
  hjust = 0.00000000000000000000000001,
  vjust = 1
) +
  theme(
    plot.margin = margin(10,10,10,10)
  ) -> plot8_3
plot8_3


ggsave(
  plot = plot8_3,
  filename = here::here('outputs/figure-04.pdf'),
  device = cairo_pdf,
  dpi = 400
)

pdf_convert(
  pdf = here::here('outputs/figure-04.pdf'),
  filenames = here::here('outputs/figure-04.jpeg'),
  format = "jpeg", dpi = 400
)
